<?php

namespace app\admin\service;

use app\admin\model\TrainChild;
use app\admin\model\TrainList;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class TrainService
{
    public static function goAddTask()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'train_title' => 'require',
                'user_id' => 'require',
                'begin_time' => 'require',
                'end_time' => 'require',
            ];
            $message = [
                'user_id.require' => "用户ID不能为空",
                'train_title.require' => "训练名称不能为空",
                'begin_time.require' => "开始时间不能为空",
                'end_time.require' => "结束时间不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            $data['begin_time'] = strtotime($data['begin_time']);
            $data['end_time'] = strtotime($data['end_time']);
            try {
                $data['train_type'] = 1;
                TrainList::create($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEditTask()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'train_id' => 'require',
                'train_title' => 'require',
                'user_id' => 'require',
                'begin_time' => 'require',
                'end_time' => 'require',
            ];
            $message = [
                'train_id.require' => "训练ID不能为空",
                'user_id.require' => "用户ID不能为空",
                'train_title.require' => "训练名称不能为空",
                'begin_time.require' => "开始时间不能为空",
                'end_time.require' => "结束时间不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['imageFile']);
            $data['begin_time'] = strtotime($data['begin_time']);
            $data['end_time'] = strtotime($data['end_time']);
            try {
                TrainList::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatusTask()
    {
        $data = Request::post();
        $model = TrainList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save(['level_status' => $data['status']]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemoveTask()
    {
        if (Request::isPost()) {
            $ids = Request::param('ids');
            try {
                TrainList::where('id', 'in', $ids)->delete();
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goAddTrain()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'train_id' => 'require',
                'video_id' => 'require',
                'train_sort' => 'require',
//                'train_times' => 'require',
            ];
            $message = [
                'train_id.require' => "训练ID不能为空",
                'video_id.require' => "视频名称不能为空",
                'train_sort.require' => "训练排序不能为空",
//                'train_times.require' => "训练次数不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $res = TrainChild::create($data);
                mkdir(app()->getRootPath() . 'public' . DS . 'upload' . DS . 'train' . DS . $res['child_id'], 0755, true);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goEditTrain()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'child_id' => 'require',
                'train_id' => 'require',
                'video_id' => 'require',
                'train_sort' => 'require',
//                'train_times' => 'require',
            ];
            $message = [
                'child_id.require' => "ID不能为空",
                'train_id.require' => "训练ID不能为空",
                'video_id.require' => "视频名称不能为空",
                'train_sort.require' => "训练排序不能为空",
//                'train_times.require' => "训练次数不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                TrainChild::update($data);
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }
}